/**
 * @Description 获取地理位置坐标
 * @author 焦政
 * @date 2019/4/26
 */
async function getcoordinate() {
    return new Promise((resolve, reject) => {
        wx.getSetting({
            success: function (e) {
                let userLocation = e.authSetting["scope.userLocation"];
                if (typeof (userLocation) == "undefined") {  // 用户第一次授权地理位置
                    //1、获取当前位置坐标
                    wx.getLocation({
                        type: 'wgs84',
                        success: res => {
                            //2、根据坐标获取当前位置名称，显示在顶部:逆地址解析
                            resolve(res)
                        }
                    })
                } else {  // 用户点了允许授权之后 又设置不允许获取位置的处理

                }
            }
        })
    })

}

/**
 * @Description 获取精确地址位置信息 逆向地理编码
 * @author 焦政
 * @date 2019/4/26
 */
async function getPosistion(coordinate) {
    return new Promise(async (resolve, reject) => {
        //百度地图
        // let url = `http://api.map.baidu.com/geocoder/v2/?location=${coordinate.latitude},${coordinate.longitude}&output=json&pois=1&ak=xOfxhXZXdRnKiKNROOgkGFMCcGlT9fn0`
        //高德地图
        let url = `https://restapi.amap.com/v3/geocode/regeo?key=e80eace082b331e7da1bce2f3d7b73ec&location=${coordinate.longitude},${coordinate.latitude}`
        //腾讯地图
        // let url = `http://apis.map.qq.com/ws/geocoder/v1/?location=${coordinate.latitude},${coordinate.longitude}&key=BB3BZ-VLD33-UR237-YL3EB-OKZCK-2FBOG`
        wx.request({
            url: url,
            // method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded', // 默认值
            },
            success(res) {
                let keyList = ['longitude', 'latitude', 'province', 'city', 'district', 'street', 'streetnumber', 'position']
                //如果当前城市没有 则自动填入省份  为了应对像北京上海类似的直辖市
                if(res.data.regeocode.addressComponent.city.length == 0){
                    res.data.regeocode.addressComponent.city = res.data.regeocode.addressComponent.province
                }
                //填充具体地理位置
                if(res.data.regeocode.addressComponent.neighborhood.name.length == 0){
                    res.data.regeocode.addressComponent.neighborhood.name = res.data.regeocode.addressComponent.township
                }
                let paramsList = {
                    longitude: coordinate.longitude,
                    latitude: coordinate.latitude,
                    province: res.data.regeocode.addressComponent.province,
                    city: res.data.regeocode.addressComponent.city,
                    district: res.data.regeocode.addressComponent.district,
                    street: res.data.regeocode.addressComponent.streetNumber.street,
                    streetnumber: res.data.regeocode.addressComponent.streetNumber.number,
                    position: res.data.regeocode.addressComponent.neighborhood.name
                }
                // let paramsList = {
                //     longitude: coordinate.longitude,
                //     latitude: coordinate.latitude,
                //     province: res.data.result.address_component.province,
                //     city: res.data.result.address_component.city,
                //     district: res.data.result.address_component.district,
                //     street: res.data.result.address_component.street,
                //     streetnumber: res.data.result.address_component.street_number,
                //     position: res.data.result.formatted_addresses.recommend
                // }
                saveStorage(keyList, paramsList)
                resolve(paramsList)
            }
        })
    })
}

/**
 * @Description 存储数据
 * @author 焦政
 * @date 2019/4/27
 */
function saveStorage(keyList, paramsList) {
    keyList.forEach(v => {
        wx.setStorageSync(v, paramsList[v])
    })
}

/**
 * @Description 获取附近地理位置
 * @author 焦政
 * @date 2019/4/27
 */
async function getNearByAddress(longitude, latitude, positionDes = '') {
    return new Promise((resolve, reject) => {
        wx.getLocation({
            type: 'wgs84',
            success: function (res) {
                let url = `https://restapi.amap.com/v3/place/around?key=e80eace082b331e7da1bce2f3d7b73ec&keywords=${positionDes}&location=${longitude},${latitude}`
                wx.request({
                    url: url,
                    success(res) {
                        resolve(res.data.pois)
                    },
                })
            },
        })
    })
}

/**
 * @Description 搜索全国的地理位置
 * @author 焦政
 * @date 2019/4/28
 */
async function getAllAddress(longitude, latitude, seach = '', city = '') {
    return new Promise((resolve, reject) => {
        let url = `https://restapi.amap.com/v3/assistant/inputtips?key=e80eace082b331e7da1bce2f3d7b73ec&keywords=${seach}&location=${longitude},${latitude}&city=${city}&datatype=all`
        wx.request({
            url: url,
            success(res) {
                var arr = res.data.tips

                resolve(arr)
            },
        })
    })
}


export {getcoordinate, getPosistion, getNearByAddress,getAllAddress}
